package com.lyh.web.controller.app;

import com.lyh.common.annotation.Log;
import com.lyh.common.core.controller.BaseController;
import com.lyh.common.core.domain.AjaxResult;
import com.lyh.common.core.domain.entity.SysUser;
import com.lyh.common.core.page.TableDataInfo;
import com.lyh.common.enums.BusinessType;
import com.lyh.common.utils.CommonUtils;
import com.lyh.common.utils.SecurityUtils;
import com.lyh.common.utils.poi.ExcelUtil;
import com.lyh.system.domain.OperationInfo;
import com.lyh.system.domain.req.OPerPageReq;
import com.lyh.system.domain.vo.Income2Vo;
import com.lyh.system.domain.vo.IncomeVo;
import com.lyh.system.domain.vo.SevenIncomeVo;
import com.lyh.system.mapper.OperationInfoMapper;
import com.lyh.system.service.IOperationInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 收支明细Controller
 *
 * @author ruoyi
 * @date 2023-05-11
 */
@RestController
@RequestMapping("/operation/info")
public class OperationInfoController extends BaseController {
    @Autowired
    private IOperationInfoService operationInfoService;
    @Autowired
    private OperationInfoMapper operationInfoMapper;

    /**
     * 查询收支明细列表
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(required = false) String type, @RequestParam(required = false) String rule) {
        String ruleStr = "";
        if ("STORE".equals(rule)){
            //门店区只展示699返佣信息
            ruleStr = "STORE_INCOME";
        }else if ("ONLINE".equals(rule)){
            //线上展示699返佣和报单收益，及报单返佣
            ruleStr = "PURCHASE_INCOME,ORDINARY_INCOME,RESERVE_EVIDENCE_INCOME";
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<OperationInfo> list = operationInfoMapper.operationInfoList(user.getUserId(), type, ruleStr);
        return AjaxResult.success(list);
    }

//    /**
//     * 查询收支明细列表
//     */
//    @GetMapping("/webList")
//    @ApiOperation("人员收支明细")
//    public TableDataInfo list(OPerPageReq req) {
//
//        startPage();
//        List<OperationInfo> list = operationInfoService.operationInfoPage(req);
//        return getDataTable(list);
//    }

    @GetMapping("/webList")
    @ApiOperation("人员收支明细")
    public TableDataInfo webList(OPerPageReq req)
    {
        startPage();
        List<OperationInfo> list = operationInfoService.operationInfoPage(req);
        return getDataTable(list);

    }


    /**
     * 导出收支明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "收支明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OperationInfo operationInfo) {
        List<OperationInfo> list = operationInfoService.selectOperationInfoList(operationInfo);
        ExcelUtil<OperationInfo> util = new ExcelUtil<OperationInfo>(OperationInfo.class);
        util.exportExcel(response, list, "收支明细数据");
    }

    /**
     * 获取收支明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(operationInfoService.selectOperationInfoById(id));
    }

    /**
     * 新增收支明细
     */
    @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "收支明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OperationInfo operationInfo) {
        return toAjax(operationInfoService.insertOperationInfo(operationInfo));
    }

    /**
     * 修改收支明细
     */
    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "收支明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OperationInfo operationInfo) {
        return toAjax(operationInfoService.updateOperationInfo(operationInfo));
    }

    /**
     * 删除收支明细
     */
    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "收支明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(operationInfoService.deleteOperationInfoByIds(ids));
    }

    @GetMapping("/sevenList")
    public AjaxResult sevenList() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<String> sevenDate = CommonUtils.getSevenDate();
        List<SevenIncomeVo> list = new ArrayList<>();
        for (String date : sevenDate) {
            SevenIncomeVo siv = new SevenIncomeVo();
            String s = operationInfoMapper.sevenList(date, user.getUserId());
            siv.setIncome(s);
            siv.setIncomeDate(date);
            list.add(siv);
        }
        return AjaxResult.success(list);
    }

    /**
     * 动态收益
     */
    @GetMapping("/dynamicIncome")
    public AjaxResult dynamicIncome() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        IncomeVo incomeVo = new IncomeVo();
        List<Income2Vo> dynamicIncome = operationInfoMapper.getDynamicIncome(user.getUserId());
        incomeVo.setIncomeList(dynamicIncome);
        BigDecimal totalPrice = dynamicIncome.stream().map(Income2Vo::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        incomeVo.setSumIncome(totalPrice);
        return AjaxResult.success(incomeVo);
    }

    /**
     * 静态收益
     */
    @GetMapping("/staticIncome")
    public AjaxResult staticIncome() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        IncomeVo incomeVo = new IncomeVo();
        List<Income2Vo> dynamicIncome = operationInfoMapper.getStaticIncome(user.getUserId());
        incomeVo.setIncomeList(dynamicIncome);
        BigDecimal totalPrice = dynamicIncome.stream().map(Income2Vo::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        incomeVo.setSumIncome(totalPrice);
        return AjaxResult.success(incomeVo);
    }
}
